# BEWARE:
# This is a TARGETS<->BUCK dirsync.
#   - fbcode/thrift/lib/rust/TARGETS
#   - xplat/thrift/lib/rust/BUCK

load("@fbcode_macros//build_defs:native_rules.bzl", "alias", "buck_genrule")
load("@fbsource//tools/build_defs:default_platform_defs.bzl", "ANDROID", "APPLE", "CXX", "FBCODE", "WINDOWS")
load("@fbsource//tools/build_defs:fb_xplat_rust_library.bzl", "fb_xplat_rust_library")
load("@fbsource//tools/build_defs:fbsource_utils.bzl", "is_xplat")
load("@fbsource//tools/target_determinator/macros:ci.bzl", "ci")

oncall("thrift")

# In order to avoid potentially bringing both fbcode//thrift/lib/rust:fbthrift
# and fbsource//xplat/thrift/lib/rust:fbthrift into the same build, the
# unsuffixed target redirects back to fbsource. `fb_xplat_cxx_library` and
# `cpp_library` have a more complex dependency remapping built-in, that does
# not exist in rust yet.
#
# buildifier: disable=no-effect
alias(
    name = "fbthrift",
    actual = "fbsource//xplat/thrift/lib/rust:fbthrift",
    visibility = ["PUBLIC"],
) if not is_xplat() else None

# Mirror the autocargo manifest to fbcode to keep arc autocargo happy
buck_genrule(
    name = "fbthrift-rust-manifest",
    out = "manifest.json",
    cmd = "$(exe fbsource//tools/build_defs/genrule_utils:copyfile) $(location fbsource//xplat/thrift/lib/rust:fbthrift-rust-manifest) $OUT",
    labels = [
        "rust_manifest",
        ci.skip_target(),
    ],
) if not is_xplat() else None

# buildifier: disable=duplicated-name
fb_xplat_rust_library(
    name = "fbthrift",
    srcs = glob([
        "src/*.rs",
        "src/tests/*.rs",
    ]),
    autocargo = {
        "cargo_toml_config": {
            "package": {
                "publish": False,
                "version": "0.0.1+unstable",
            },
        },
    },
    platforms = (APPLE, ANDROID, CXX, WINDOWS, FBCODE),
    test_deps = ["fbsource//third-party/rust:quickcheck"],
    visibility = ["PUBLIC"],
    deps = [
        "fbsource//third-party/rust:anyhow",
        "fbsource//third-party/rust:async-trait",
        "fbsource//third-party/rust:base64",
        "fbsource//third-party/rust:bufsize",
        "fbsource//third-party/rust:bytes",
        "fbsource//third-party/rust:futures",
        "fbsource//third-party/rust:ghost",
        "fbsource//third-party/rust:num-derive",
        "fbsource//third-party/rust:num-traits",
        "fbsource//third-party/rust:ordered-float",
        "fbsource//third-party/rust:panic-message",
        "fbsource//third-party/rust:serde_json",
        "fbsource//third-party/rust:thiserror",
    ],
) if is_xplat() else None
